import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * Created by dllo on 18/5/2.
 */
public class Main {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager
                    .getConnection("jdbc:mysql://localhost:3306/lanou3", "root", "123456");
            // 开启事务
            // 把autoCommit关闭,相当于使用了begin语句
            conn.setAutoCommit(false);
            String sql1 = "UPDATE bank SET money = money - 5 WHERE cname = '鸣人'";
            String sql2 = "UPDATE bank SET money = money + 5 WHERE cname = '佐助'";
            Statement statement = conn.createStatement();
            statement.executeUpdate(sql1); // 执行SQL1
            statement.executeUpdate(sql2); // 执行SQL2

            conn.commit(); // 提交事务

            statement.close(); // 关闭资源
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
